#!user/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import time
import datetime
import threading
import pymongo
import constant
from apscheduler.schedulers.blocking import BlockingScheduler

def zbsj_request(range,ct):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
    }
    url = 'https://sj.zhiboclub.com/api/chart/share_data?_t%20=' + str(int(time.time() * 1000))
    datalist = [{"df_id":"df_061d3c1ba48d3e1c436641791cf0a572","range":[range],"data_type":"string","granularity":"","range_type":"1"}]
    post_data ={
      'ct_id': (None,ct),
      'linked_chart_type': (None,0),
      'chart_jump_info': (None,json.dumps({})),
      'sdo_id': (None,'sdo_a746c8739cd240c8fe9745344ca4f9fa'),
      'dsh_filter': (None,json.dumps(datalist)),
      '_t': (None,str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")))
    }
    # # 代理服务器
    # proxyHost = "http-dyn.abuyun.com"
    # proxyPort = "9020"
    #
    # # 代理隧道验证信息
    # proxyUser = "HH9M77VC14FM5Y2D"
    # proxyPass = "14E09EC1E063DBF3"
    # proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
    #     "host": proxyHost,
    #     "port": proxyPort,
    #     "user": proxyUser,
    #     "pass": proxyPass,
    # }
    # proxies = {
    #     "http": proxyMeta,
    #     "https": proxyMeta,
    # }
    response = requests.post(url,headers=headers,files=post_data)
    return json.loads(response.text)

def get_list_data(range):
    ct_id_list = [
        'ct_4b0ef31a2dc2e6a82bbbe62d32d5a664',
        'ct_e947ef072f7531738976ae3960ce9c8d',
        'ct_b87a77f6ca7f8771415b3ee7093615c9',
        'ct_e947ef072f7531738976ae3960ce9c8d',
        'ct_ddf0c3258ad9397ddb166d0e207ef761',
    ]
    all_data = []
    for ct in ct_id_list:
        all_data.append(zbsj_request(range,ct))

    return all_data

def zbsj_detail_request(range,ct):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
    }
    url = 'https://sj.zhiboclub.com/api/chart/share_data?_t%20=' + str(int(time.time() * 1000))
    datalist = [{"df_id":"df_061d3c1ba48d3e1c436641791cf0a572", "range":[range], "data_type":"string", "granularity":"", "range_type":"1"}]
    post_data ={
      'ct_id': (None,ct),
      'linked_chart_type': (None,0),
      'chart_jump_info': (None,json.dumps({})),
      'sdo_id': (None,'sdo_a746c8739cd240c8fe9745344ca4f9fa'),
      'dsh_filter': (None,json.dumps(datalist)),
      '_t': (None,str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")))
    }
    # # 代理服务器
    # proxyHost = "http-dyn.abuyun.com"
    # proxyPort = "9020"
    #
    # # 代理隧道验证信息
    # proxyUser = "HH9M77VC14FM5Y2D"
    # proxyPass = "14E09EC1E063DBF3"
    # proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
    #     "host": proxyHost,
    #     "port": proxyPort,
    #     "user": proxyUser,
    #     "pass": proxyPass,
    # }
    # proxies = {
    #     "http": proxyMeta,
    #     "https": proxyMeta,
    # }
    try:
        response = requests.post(url,headers=headers,files=post_data)
        return json.loads(response.text)
    except:
        return None

def get_detail_data(range):
    ct_id_list = [
        'ct_e2a0a16e287bbb5de18095c8abaa8050',
    ]
    all_data = []
    for ct in ct_id_list:
        all_data.append(zbsj_detail_request(range,ct))

    return all_data

def zbsj_request_proxies(rang,ct):
    for i in range(3):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
            }
            url = 'https://sj.zhiboclub.com/api/chart/share_data?_t%20=' + str(int(time.time() * 1000))
            datalist = [{"df_id":"df_061d3c1ba48d3e1c436641791cf0a572","range":[rang],"data_type":"string","granularity":"","range_type":"1"}]
            post_data ={
              'ct_id': (None,ct),
              'linked_chart_type': (None,0),
              'chart_jump_info': (None,json.dumps({})),
              'sdo_id': (None,'sdo_a746c8739cd240c8fe9745344ca4f9fa'),
              'dsh_filter': (None,json.dumps(datalist)),
              '_t': (None,str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")))
            }
            # 代理服务器
            proxyHost = "http-dyn.abuyun.com"
            proxyPort = "9020"

            # 代理隧道验证信息
            proxyUser = "HH9M77VC14FM5Y2D"
            proxyPass = "14E09EC1E063DBF3"
            proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
                "host": proxyHost,
                "port": proxyPort,
                "user": proxyUser,
                "pass": proxyPass,
            }
            proxies = {
                "http": proxyMeta,
                "https": proxyMeta,
            }
            response = requests.post(url,headers=headers,files=post_data,proxies=proxies)
            return json.loads(response.text)
        except:
            continue

def get_list_data_proxies(range):
    ct_id_list = [
        'ct_4b0ef31a2dc2e6a82bbbe62d32d5a664',
        'ct_e947ef072f7531738976ae3960ce9c8d',
        'ct_b87a77f6ca7f8771415b3ee7093615c9',
        'ct_e947ef072f7531738976ae3960ce9c8d',
        'ct_ddf0c3258ad9397ddb166d0e207ef761',
    ]
    all_data = []
    for ct in ct_id_list:
        all_data.append(zbsj_request_proxies(range,ct))

    return all_data

def zbsj_detail_request_proxies(rang,ct):
    for i in range(3):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36'
            }
            url = 'https://sj.zhiboclub.com/api/chart/share_data?_t%20=' + str(int(time.time() * 1000))
            datalist = [{"df_id":"df_061d3c1ba48d3e1c436641791cf0a572","range":[rang],"data_type":"string","granularity":"","range_type":"1"}]
            post_data ={
              'ct_id': (None,ct),
              'linked_chart_type': (None,0),
              'chart_jump_info': (None,json.dumps({})),
              'sdo_id': (None,'sdo_a746c8739cd240c8fe9745344ca4f9fa'),
              'dsh_filter': (None,json.dumps(datalist)),
              '_t': (None,str(datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")))
            }
            # 代理服务器
            proxyHost = "http-dyn.abuyun.com"
            proxyPort = "9020"

            # 代理隧道验证信息
            proxyUser = "HH9M77VC14FM5Y2D"
            proxyPass = "14E09EC1E063DBF3"
            proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
                "host": proxyHost,
                "port": proxyPort,
                "user": proxyUser,
                "pass": proxyPass,
            }
            proxies = {
                "http": proxyMeta,
                "https": proxyMeta,
            }
            response = requests.post(url,headers=headers,files=post_data,proxies=proxies)
            return json.loads(response.text)
        except:
            continue

def get_detail_data_proxies(range):
    ct_id_list = [
        'ct_e2a0a16e287bbb5de18095c8abaa8050',
    ]
    all_data = []
    for ct in ct_id_list:
        all_data.append(zbsj_detail_request_proxies(range,ct))

    return all_data


def run(skip):
    client = pymongo.MongoClient(constant.MONGO_URL)
    db = client.WeChat
    col = db.zbsj_list
    coll = db.zbsj_detail

    anchorlist = []
    for i in col.find().limit(25000).skip(25000*skip):
        anchorlist.append(i['anchorName'])

    for one in anchorlist:
        isfinish = 0
        for i in range(5):
            try:
                old_data = col.find_one({'anchorName':one})
                old_data.pop('_id')

                if old_data['isGet'] == 1:
                    up_date = get_list_data_proxies(one)
                    # if up_date[0]['result']['data']['y'][0]['data'][0] == []:
                    #     up_date = get_list_data_proxies(one)
                    try:
                        old_data['anchorId'] = up_date[1]['result']['data']['x'][1]['data'][0]
                        isfinish+=1
                    except:
                        old_data['anchorId'] = ''
                        break
                    try:
                        old_data['fansNumber'] = int(up_date[0]['result']['data']['y'][0]['data'][0])
                        isfinish+=1
                    except:
                        old_data['fansNumber'] = ''
                    try:
                        old_data['institution'] = up_date[2]['result']['data']['x'][0]['data'][0]
                        isfinish+=1
                    except:
                        old_data['institution'] = ''
                    try:
                        old_data['level'] = up_date[3]['result']['data']['x'][4]['data'][0]
                        isfinish+=1
                    except:
                        old_data['level'] = ''
                    try:
                        old_data['isshop'] = up_date[4]['result']['data']['x'][0]['data'][0]
                        isfinish+=1
                    except:
                        old_data['isshop'] = ''

                    #详细数据
                    data = get_detail_data_proxies(one)
                    # if data[0]['result']['data']['x'][0]['data'] == []:
                    #     data = get_detail_data_proxies(one)
                    try:
                        old_data['kbsj'] = data[0]['result']['data']['x'][0]['data']
                    except:
                        old_data['kbsj'] = []
                        break
                    try:
                        old_data['kbsj_h'] = data[0]['result']['data']['x'][1]['data']
                    except:
                        old_data['kbsj_h'] = []
                    try:
                        old_data['mxsgkrs'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][0]['data']]
                    except:
                        old_data['mxsgkrs'] = []
                    try:
                        old_data['mxsgks'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][1]['data']]
                    except:
                        old_data['mxsgks'] = []
                    try:
                        old_data['gkrs'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][2]['data']]
                    except:
                        old_data['gkrs'] = []
                    try:
                        old_data['gks'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][3]['data']]
                    except:
                        old_data['gks'] = []
                    try:
                        old_data['sc'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][4]['data']]
                    except:
                        old_data['sc'] = []
                    try:
                        old_data['hds'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][5]['data']]
                    except:
                        old_data['hds'] = []
                    try:
                        old_data['mrzfs'] = [int(x.split('.')[0]) for x in data[0]['result']['data']['y'][6]['data']]
                    except:
                        old_data['mrzfs'] = []
                    old_data['isFinished'] = 1
                    if isfinish == 0 and old_data['kbsj'] == [] and old_data['kbsj_h'] == [] and old_data['mxsgkrs'] == [] and old_data['mxsgks'] == [] and old_data['gkrs'] == [] and old_data['gks'] == [] and old_data['sc'] == [] and old_data['hds'] == [] and old_data['mrzfs'] == []:
                        old_data['isFinished'] = 0
                    old_data['updateTime'] = str(datetime.date.today())
                    print(old_data)
                    # 更新数据
                    coll.update({'anchorName':one}, old_data,True)
                    break
            except:
                continue

def main():
    start = time.time()
    threads = []

    for i in range(5):
        threads.append(threading.Thread(target=run,args=(i,)))

    #运行线程
    for th in threads:
        th.start()

    #等待线程退出
    for th in threads:
        th.join()
    end = time.time()
    print(datetime.date.today())
    print(end-start)

if __name__ == '__main__':
    scheduler = BlockingScheduler()

    scheduler.add_job(main, 'cron', day='*/1', hour=8, minute=50, misfire_grace_time=3600)

    scheduler.start()